#include <time.h>
#include <winsock2.h>
#include "MySQL/mysql.h"
#include "pThreads/semaphore.h"
#include <vector>
#include "CSemaphore.h"

#include "CZoneHandler.h"
#include "CSpawnPointHandler.h"
#include "CLuaManager.h"
#include "CUtil.h"


#include "CPacket.h"
#include "CDb.h"
#include "CConsole.h"
#include "CZone.h"



CZone::CZone(unsigned short zoneID) {
	
	this->m_zoneID = zoneID;

	this->loadZoneLines();

	this->loadZoneMusic();

	//Clear zone weather to be default no weather
	memset(&this->m_zoneWeather, 0 , sizeof(zoneWeather));
}

CZone::~CZone() {
}

unsigned short CZone::getId() {
	return this->m_zoneID;
}

void CZone::loadZoneLines() {

	MYSQL_RES * res = NULL;
	MYSQL_ROW row;
	char query[8192];
	int count;

	sprintf(query, "SELECT fromline, tozone, tox, toy, toz, rotation FROM zoneline WHERE fromzone = 0x%x ",this->m_zoneID);

	count = (int) strlen(query);
	MYSQL* mysqlConn=CDb::getConn(); //get conn
	mysql_real_query(mysqlConn, query, count);

	res = mysql_store_result(mysqlConn);
	CDb::pushConn(mysqlConn); //release conn

	if(!res) {
		CConsole::outErr("Error looking up characters inventory");
		return;
	}
	if(!mysql_num_rows(res)){

	}
	else {
		while((row = mysql_fetch_row(res))) {

			struct zoneLine zl;
			memset(zl.m_zoneLineIdent, 0, 5); //TODO: zl.m_zoneLineIdent is size 4 or 5 ?
			memcpy(zl.m_zoneLineIdent, row[0], 5); //TODO: zl.m_zoneLineIdent is size 4 or 5 ?
			memcpy(&zl.m_toZone, row[1], 1);
			memcpy(&zl.m_toPos.x, row[2], 4);
			memcpy(&zl.m_toPos.y, row[3], 4);
			memcpy(&zl.m_toPos.z, row[4], 4);
			memcpy(&zl.m_toPos.rotation, row[5], 1);

			this->m_zoneLineList.push_back(zl);

		//	CConsole::outTime("%s %X ",zl.m_zoneLineIdent, zl.m_toZone);
		}
	}
	mysql_free_result(res);


}

void CZone::setZoneWeather(struct weather newWeather) 
{
	memcpy(&this->m_zoneWeather.m_lastWeather,&this->m_zoneWeather.m_currentWeather, sizeof(struct weather));
	memcpy(&this->m_zoneWeather.m_currentWeather,&newWeather,sizeof(struct weather));
}

struct weather CZone::getLastWeather()
{
	return this->m_zoneWeather.m_lastWeather;
}

struct weather CZone::getCurrentWeather()
{
	return this->m_zoneWeather.m_currentWeather;
}

unsigned long long CZone::getNextWeatherChange()
{
	return this->m_zoneWeather.m_nextWeatherChange;
}

void CZone::setNextWeatherChange(unsigned long long nextChangeTime)
{
	this->m_zoneWeather.m_nextWeatherChange = nextChangeTime;
}

void CZone::loadZoneMusic() {

	MYSQL_RES * res;
	MYSQL_ROW row;
	char query[1024];
	strcpy(query, "SELECT sng1, sng2, battlesolo, battlemulti FROM zone_settings WHERE Zoneid = '");
	int count = (int) strlen(query);

	MYSQL* mysqlConn=CDb::getConn(); //get conn
	count += mysql_real_escape_string(mysqlConn, query+count, (char *)&(this->m_zoneID), 1);
	strcpy(query+count, "' LIMIT 1");
	count = (int) strlen(query);

	mysql_real_query(mysqlConn, query, count);

	res = mysql_store_result(mysqlConn);
	CDb::pushConn(mysqlConn); //release conn

	if (res) 
	{
		if(row = mysql_fetch_row(res)) 
		{
			this->m_zoneMusic.m_song1  = (unsigned char)row[0][0];	// background music
			this->m_zoneMusic.m_song2  = (unsigned char)row[0][2];	// background 2
			this->m_zoneMusic.m_bSongS = (unsigned char)row[0][4];	// solo battle music
			this->m_zoneMusic.m_bSongM = (unsigned char)row[0][6];	// party battle music
		}
		mysql_free_result(res);
	}

}